home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / adx7mu1a / lobby.frm < prev    next >
Text File  |  1999-10-07  |  7KB  |  263 lines

  1. VERSION 5.00
  2. Begin VB.Form Lobby 
  3.    Caption         =   "Tic Tac Lobby"
  4.    ClientHeight    =   3195
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   4680
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   3195
  10.    ScaleWidth      =   4680
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.TextBox GameName 
  13.       Height          =   375
  14.       Left            =   480
  15.       TabIndex        =   3
  16.       Text            =   "Text1"
  17.       Top             =   2040
  18.       Width           =   2415
  19.    End
  20.    Begin VB.CommandButton startgame 
  21.       Caption         =   "Start Game"
  22.       Height          =   495
  23.       Left            =   3360
  24.       TabIndex        =   2
  25.       Top             =   1680
  26.       Width           =   1095
  27.    End
  28.    Begin VB.CommandButton join 
  29.       Caption         =   "Join"
  30.       Height          =   495
  31.       Left            =   3360
  32.       TabIndex        =   1
  33.       Top             =   600
  34.       Width           =   1095
  35.    End
  36.    Begin VB.ListBox playerlist 
  37.       Height          =   1230
  38.       Left            =   360
  39.       TabIndex        =   0
  40.       Top             =   720
  41.       Width           =   2655
  42.    End
  43. End
  44. Attribute VB_Name = "Lobby"
  45. Attribute VB_GlobalNameSpace = False
  46. Attribute VB_Creatable = False
  47. Attribute VB_PredeclaredId = True
  48. Attribute VB_Exposed = False
  49. Option Explicit
  50.  
  51. Private Sub cmdCancel_Click()
  52.   Hide
  53.  ' Unload frmMultiplayer  ' Force reset of DPlay
  54.   'frmMainMenu.Show
  55. End Sub
  56.  
  57. Private Sub cmdRefresh_Click()
  58.     Screen.MousePointer = vbHourglass
  59.     UpdateSessionList
  60.     Screen.MousePointer = vbNormal
  61. End Sub
  62.  
  63. Private Sub Form_Unload(Cancel As Integer)
  64.   cmdCancel_Click
  65. End Sub
  66.  
  67. Private Sub cmdCreate_Click()
  68.   Hide
  69.  ' frmCreateGame.Show
  70.     ' Put focus on name
  71.   'frmCreateGame.txtGameName.SetFocus
  72.  
  73. End Sub
  74.  
  75. ' Join game
  76.  
  77. Private Sub cmdJoin_Click()
  78.  
  79.   Dim SessionData As DirectPlaySessionData
  80.   
  81.   ' Join selected session
  82.   'Set SessionData = gObjDPEnumSessions.GetItem(lstSessions.ListIndex + 1)
  83.   On Error GoTo NOSESSION
  84.  ' Call gObjDPlay.Open(SessionData, DPOPEN_JOIN)
  85.   On Error GoTo 0
  86.   
  87.   ' Create player
  88.   Dim PlayerHandle As String
  89.   Dim PlayerName As String
  90.   
  91.   'PlayerName = frmMultiplayer.txtYourName.Text
  92.   PlayerHandle = "Player"    ' We don't use this
  93.   
  94.   ' Create the player
  95.   On Error GoTo FAILEDCREATE
  96.   'gMyPlayerID = gObjDPlay.CreatePlayer(PlayerName, PlayerHandle, 0, 0)
  97.   On Error GoTo 0
  98.   
  99.   ' Only host can start game, so disable Start button
  100.   'frmWaiting.cmdStart.Enabled = False
  101.   
  102.   ' Hide this and show status window
  103.   Hide
  104.   'frmWaiting.Show
  105.   'frmWaiting.UpdateWaiting  ' show the player list
  106.   Exit Sub
  107.  
  108. ' Error handlers
  109.  
  110. NOSESSION:
  111.   MsgBox ("Failed to join game.")
  112.   UpdateSessionList
  113.   Exit Sub
  114.   
  115. FAILEDCREATE:
  116.   MsgBox ("Failed to create player.")
  117.   Exit Sub
  118.   
  119. End Sub
  120.  
  121.  
  122. ' Update session listbox.
  123.  
  124. Public Function UpdateSessionList() As Boolean
  125.   
  126.   Dim SessionCount As Integer, x As Integer
  127.   Dim SessionData As DirectPlaySessionData
  128.   Dim Details As String
  129.   
  130.   ' Delete the old list
  131.   playerlist.Clear
  132.   
  133.   Set SessionData = dxplay.CreateSessionData
  134.   
  135.   ' Enumerate the sessions in synchronous mode.
  136.   Call SessionData.SetGuidApplication(AppGuid)
  137.   Call SessionData.SetSessionPassword("")
  138.   
  139.   If usermode = "host" Then
  140.   Set SessionData = dxplay.CreateSessionData
  141. ' Finish describing the session
  142.   Call SessionData.SetSessionName(GameName.Text)
  143.   Call SessionData.SetGuidApplication(AppGuid)
  144.   Call SessionData.SetFlags(DPSESSION_MIGRATEHOST)
  145.   
  146.   ' Create (and join) the session.
  147.   
  148.   ' Failure can result from the user cancelling out of the service provider dialog.
  149.   ' In the case of the modem, this is the "answer" dialog.
  150.   Call dxplay.Open(SessionData, DPOPEN_CREATE)
  151.   
  152.   ' Describe the host player
  153.   Dim PlayerHandle As String
  154.   Dim PlayerName As String
  155.   
  156.   PlayerName = connectfrm.PlayersName.Text
  157.   PlayerHandle = "Player 1 (Host)"
  158.   
  159.   ' Create the host player
  160.   MyPlayer = dxplay.CreatePlayer(PlayerName, PlayerHandle, 0, 0)
  161.   On Error GoTo 0
  162.     
  163.   dxHost = True
  164.   End If
  165.   
  166.   
  167.   
  168.   
  169.   
  170.   
  171.   
  172.   
  173.   On Error GoTo USERCANCEL
  174.   Set EnumSessions = dxplay.GetDPEnumSessions(SessionData, 0, _
  175.           DPENUMSESSIONS_AVAILABLE)
  176.   On Error GoTo 0
  177.  
  178.   ' List info for enumerated sessions: name, players, max. players
  179.   
  180.   On Error GoTo ENUM_ERROR
  181.   SessionCount = EnumSessions.GetCount
  182.   For x = 1 To SessionCount
  183.     Set SessionData = EnumSessions.GetItem(x)
  184.     Details = SessionData.GetSessionName & " (" & SessionData.GetCurrentPlayers _
  185.             & "/" & SessionData.GetMaxPlayers & ")"
  186.     playerlist.AddItem (Details)
  187.   Next x
  188.   
  189.   ' Update Join button
  190.   If SessionCount = 0 Then
  191.    join.Enabled = False
  192.   Else
  193.     join.Enabled = True
  194.   End If
  195.   
  196.   ' Initialize selection
  197.   If playerlist.ListCount > 0 Then playerlist.ListIndex = 0
  198.   
  199.   UpdateSessionList = True
  200.   Exit Function
  201.   
  202.   ' Error handlers
  203.   ' User cancelled out of service provider dialog, e.g. for modem connection.
  204.   ' We can't enumerate sessions but the user can still host a game.
  205. USERCANCEL:
  206.   UpdateSessionList = False
  207.   join.Enabled = False
  208.   Exit Function
  209.   
  210. ENUM_ERROR:
  211.   UpdateSessionList = False
  212.   MsgBox ("Error in enumeration functions.")
  213.   Exit Function
  214.   
  215. End Function
  216. Private Sub startgame_Click()
  217.  Dim ConnectionMade As Boolean
  218. Dim dxAddress As DirectPlayAddress
  219.  Dim SessionData As DirectPlaySessionData
  220. Set SessionData = dxplay.CreateSessionData
  221. ' Finish describing the session
  222.   Call SessionData.SetSessionName(GameName.Text)
  223.   Call SessionData.SetGuidApplication(AppGuid)
  224.   Call SessionData.SetFlags(DPSESSION_MIGRATEHOST)
  225.   
  226.   ' Create (and join) the session.
  227.   Dim cindex As Long
  228.   
  229.   ' Failure can result from the user cancelling out of the service provider dialog.
  230.   ' In the case of the modem, this is the "answer" dialog.
  231.   cindex = connectfrm.connectiontype.ListIndex + 1
  232.   Set dxAddress = EnumConnections.GetAddress(cindex)
  233.   Call dxplay.InitializeConnection(dxAddress)
  234.   ConnectionMade = Lobby.UpdateSessionList
  235.   If ConnectionMade Then
  236.     Hide
  237.     Lobby.Show
  238.   Else
  239.     InitDPlay
  240.   End If
  241.   Exit Sub
  242.   ' Error handlers
  243. INITIALIZEFAILED:
  244.   If Err.Number <> DPERR_ALREADYINITIALIZED Then
  245.     MsgBox ("Failed to initialize connection.")
  246.     Exit Sub
  247.   End If
  248.   
  249.   'Call dxplay.Open(SessionData, DPOPEN_CREATE)
  250.   ' Describe the host player
  251.   Dim PlayerHandle As String
  252.   Dim PlayerName As String
  253.   
  254.   PlayerName = connectfrm.PlayersName.Text
  255.   PlayerHandle = "Player 1 (Host)"
  256.   
  257.   ' Create the host player
  258.   MyPlayer = dxplay.CreatePlayer(PlayerName, PlayerHandle, 0, 0)
  259.   On Error GoTo 0
  260.     
  261.   dxHost = True
  262. End Sub
  263.